package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.vo.CommentVO;
import cn.tedu.weibo.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/5/17 15:46
 * @Package 包名：cn.tedu.weibo.controller
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
@RestController
@RequestMapping("/comment/")
public class CommentController {
	@Autowired(required = false)
	CommentMapper mapper;
	@RequestMapping("insert")
	public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
		UserVO userVO = (UserVO) session.getAttribute("user");
		if (userVO==null){
			return 2;
		}
		Comment c = new Comment();
		BeanUtils.copyProperties(commentDTO,c);
		c.setCreated(new Date());
		c.setUserId(userVO.getId());
		mapper.insert(c);
		return 1;
	}
	@RequestMapping("selectByWeiboId")
	public List<CommentVO> selectByWeiboId(int id){

		return mapper.selectByWeiboId(id);
	}
}